#-*- coding: utf-8 -*-

from utils.common import trace_function

__author__ = 'sysprog'

DEBUG = False

@trace_function
def __control_31(*arg, **kwargs):
    from utils.checksum_checker import ChecksumChecker
    import clang.cindex as ci
    from utils.unused import FunctionsChecker31
    level = kwargs["level"]
    config = kwargs["config"]

    index = ci.Index.create()

    pfname = config.get("Common", "source_file")

    tu = index.parse(pfname)

    context_dict = {"header": "3.1 Control", }


    if level <= 4:
        csc = ChecksumChecker()
        checksums = config.get("Control_31", "checksums")
        csc.load_from_string(checksums)

        context_dict.update({"label4": u"Сравнение файлов проекта с контрольными суммами",
                             "list4": csc.cs_result})
    if level <= 3:
        fsc = FunctionsChecker31()
        signatures = config.get("Control_31", "signatures")
        fsc.check_functions(tu, signatures)
        context_dict.update({"list3_both": fsc.in_both,
                             "list3_in_func": fsc.only_in_func,
                             "list3_in_sign": fsc.only_in_sign,
                             "label3": "Function checking:"})

    return context_dict


@trace_function
def __control_32(*args, **kwargs):
    from utils.control_compiled_csum import ControlCompiledCheckSum as CCCS

    level = kwargs["level"]
    config = kwargs["config"]
    context_dict = {}

    if level <= 4:
        compile_command_string = config.get("Control_32", "compile_command").split(",")
        checksum = config.get("Control_32", "checksum").strip("\"")
        c = CCCS()

        c.compile(compile_command_string)

        is_equal = checksum == c.checksum
        context_dict.update({"is_equal": is_equal, "actual_cs": c.checksum, "expected_cs": checksum})

    return context_dict


@trace_function
def __control_33(*args, **kwargs):
    import clang.cindex as ci
    from utils import func_graph
    import os

    level = kwargs["level"]
    config = kwargs["config"]

    context_dict = {"name": "Control33",}

    if level <= 3:
        index = ci.Index.create()

        filename = config.get("Common", "source_file")
        tu = index.parse(filename)

        g = func_graph.FunctionCallDepend()

        g.load_from_tu(tu)

        report_dir = config.get("Common", "report_dir")
        image_file_name = os.path.abspath(os.path.join(report_dir, "img", "control_33.svg"))
        g.render_image(image_file_name)
        context_dict.update({"image_file": os.path.join("img", "control_33.svg"), })

    return context_dict


@trace_function
def __control_34(*args, **kwargs):
    import clang.cindex as ci
    from utils.control_func_info import ControlFuncInfo
    import os

    level = kwargs["level"]
    config = kwargs["config"]

    context_dict = {"name": "Control34", }


    if level <= 3:
        index = ci.Index.create()

        filename = config.get("Common", "source_file")
        tu = index.parse(filename)

        cfi = ControlFuncInfo()

        cfi.load_from_tu(tu)
        image_file_name = os.path.abspath(os.path.join(config.get("Common", "report_dir"), "img", "control_34.svg"))
        cfi.render_image(image_file_name)

        context_dict.update({"image_file": os.path.join(".", "img", "control_34.svg"), })

    return context_dict


@trace_function
def __control_35(*args, **kwargs):
    from utils.control_info_object import ControlInfoObject
    import clang.cindex as ci
    import json

    context_dict = {"header": "Control 3.5",}
    level = kwargs["level"]
    config = kwargs["config"]

    if level <= 3:
        index = ci.Index.create()

        filename = config.get("Common", "source_file")

        tu = index.parse(filename)
        cio = ControlInfoObject()

        for sec in config.items("Control_35"):
            cio.add_signature(json.loads(sec[1]))

        cio.load_from_tu(tu)
        context_dict.update({"label3": "Control information object", "usage_map": cio.usage_map,
                             "rulesets": cio.rulesets})

    return context_dict


@trace_function
def __control_36(*args, **kwargs):
    return {}


@trace_function
def __control_37(*args, **kwargs):
    from utils.control_flow import ControlFlowAnalyzer
    import os

    level = kwargs["level"]
    config = kwargs["config"]
    context_dict = { }

    if level <= 3:
        cfg = ControlFlowAnalyzer()
        cfg.run_analyzer(config.get("Common", "source_file"))
        image_dir = os.path.join(config.get("Common", "report_dir"), "img")
        cfg.render_images(image_dir)
        context_dict.update({"list_cfg": cfg.analyzer_dump})

    return context_dict


@trace_function
def __control_38(*args, **kwargs):
    return {}


@trace_function
def __control_39(*args, **kwargs):
    return {}


controls = {"control31": __control_31,
            "control32": __control_32,
            "control33": __control_33,
            "control34": __control_34,
            "control35": __control_35,
            "control36": __control_36,
            "control37": __control_37,
            "control38": __control_38,
            "control39": __control_39}